package com.keyin;

import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.keyin.protocol.CacheImageProtocol;
import com.keyin.protocol.DownloadMagazine;
import com.keyin.protocol.PastPeriodicals;
import com.keyin.protocol.Periodical;
import com.keyin.protocol.Protocol.OnJsonProtocolResult;
import com.keyin.protocol.ProtocolHelper;

public class PastPeridocalsActivity extends BaseActivity {

	private List<Periodical> peridocals;

	private BaseAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.a_past_peridocal);

		String magazineId = getIntent().getStringExtra("magazineId");

		ProtocolHelper.getPastPeriodicals(getApplicationContext(), magazineId,
				1, 20).startTrans(
				new OnJsonProtocolResult(PastPeriodicals.class) {

					@Override
					public void onResult(Object o) {
						peridocals = ((PastPeriodicals) o).magazines;
						handler.sendEmptyMessage(0);
					}

					@Override
					public void onException(Exception e) {
						e.printStackTrace();
					}
				});
	}

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			refreshUI();
		};
	};

	private void refreshUI() {
		if (adapter == null) {
			ListView listView = (ListView) findViewById(R.id.magazine_list);
			adapter = new BaseAdapter() {

				@Override
				public View getView(int position, View convertView,
						ViewGroup parent) {
					View view = null;
					if (view == null) {
						view = View.inflate(getApplicationContext(),
								R.layout.v_list_item, null);
					}

					Periodical bean = peridocals.get(position);

					ImageView img = (ImageView) view.findViewById(R.id.cover);
					String imgUrl = bean.cover_img;
					if (imgUrl != null) {
						CacheImageProtocol imgProtocol = new CacheImageProtocol(
								getApplicationContext(), imgUrl);
						imgProtocol.startTrans(img);
					}

					TextView name = (TextView) view.findViewById(R.id.name);
					name.setText(bean.name);

					TextView date = (TextView) view.findViewById(R.id.date);
					date.setText(bean.number);

					TextView intro = (TextView) view.findViewById(R.id.intro);
					intro.setText(bean.description);
					return view;
				}

				@Override
				public long getItemId(int position) {
					return 0;
				}

				@Override
				public Object getItem(int position) {
					return null;
				}

				@Override
				public int getCount() {
					return peridocals.size();
				}
			};
			listView.setAdapter(adapter);
			listView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					new DownloadMagazine(PastPeridocalsActivity.this, peridocals
							.get(position));
				}
			});

		}
		adapter.notifyDataSetChanged();
	}
}
